clear
clear matrix
clear mata
set more off
set maxvar 9000
use "C:\Users\Andy baker\OneDrive - UCB-O365\Stata\Barry\Final\OmnibusBoth.dta", clear

fillin id wave
xtset id wave
recode respond .=0
drop if suspect==1

recode bairroibge  129=. 200=. 202=. 205=. 211=. 213=. 256=. 287=. 307=.
recode bairroamob  129=. 200=. 202=. 205=. 211=. 213=. 256=. 287=. 307=.

*converse
recode v16b_conversenotamob v16e_conversefriends v16g_conversefamily (1=3) (2=2) (3=1) (4=0) (8=.) (9=.) (10=.) (99=.)

polychoricpca v16b_conversenotamob v16e_conversefriends v16g_conversefamily, score(conversetemp) nscore(1) 
impute conversetemp v16b_conversenotamob v16e_conversefriends v16g_conversefamily if respond==1, gen(converse)

*SES
*SES
capture program drop getaggs
program define getaggs

while "`1'"~="" {
egen `1'sum=sum(`1') if respond==1, by(bairroamob wave)
gen `1'smnon=`1'sum-`1'
gen n`1'=0
replace n`1'=1 if `1'~=.
egen sumn`1'=sum(n`1') if respond==1, by(bairroamob wave)
gen `1'bairronomr=`1'smnon/(sumn`1'-1)
gen `1'bairroall=`1'sum/sumn`1' 
bysort bairroamob wave: egen `1'bairroallmean=max(`1'bairroall)
replace `1'bairronomr=`1'bairroallmean if `1'bairronomr==.
drop `1'sum n`1' sumn`1' `1'smnon `1'bairroall `1'bairroallmean
mac shift
}
end

mvdecode s6_education, mv(18)
mvdecode s6_education, mv(19)
mvdecode s12_incomefamily, mv(1)
mvdecode s12_incomefamily, mv(8)
mvdecode s12_incomefamily, mv(9)
replace s12_incomefamily=10000 if s12_incomefamily>10000 & s12_incomefamily~=.
recode s12_incomefamily 0=.
gen educ=ln(s6_education)
gen lnincome=ln(s12_incomefamily+1)
getaggs lnincome

recode v22c_presvote 2=0 4=1 else=., gen(serra_lula)
replace serra_lula=. if wave~=3

*Woman
recode s1_sex 2=1 1=0, gen(woman)

*GENERATE MEDIA ATTENTION SCORE (IV)
replace v7b1fq_mediatv1choicefreq=0 if v7a_mediatvyesorno==2
replace v7b2fq_mediatv2choicefreq=0 if v7a_mediatvyesorno==2
replace v8b1fq_mediamags1choicefreq=0 if v8a_mediamagsyesorno==2
replace v8b2fq_mediamags2choicefreq=0 if v8a_mediamagsyesorno==2
replace v9b1fq_mediapaper1choicefreq=0 if v9a_mediapaperyesorno==2
replace v9b2fq_mediapaper2choicefreq=0 if v9a_mediapaperyesorno==2
replace v10b_mediaradiofreq=10 if v10b_mediaradiofreq>10 & v10b_mediaradiofreq<61
replace v10b_mediaradiofreq=0 if v10a_mediaradioyesorno==2

mvdecode v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq, mv(8)
mvdecode v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq, mv(9)

factor v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq if respond==1, pcf factors(1) 
predict mediaattentiontemp 
impute mediaattentiontemp  v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq if respond==1, gen(mediaattention)

*Evangelicals
recode s8b_religionfreqattend 4=0 3=1 2=2 1=3 8=0 9=0, gen(attend)
gen evangelical_attend=0 if respond==1
replace evangelical_attend=attend if s8a_religionaffiliation==2 
 
*Set up Vote Variables
recode v22c_presvote 1=1 2=2 3=16 4=4 5=5 6=16 7=16 8=16 9=16 10=10 11=16 12=12 13=16 14=16 16=16 17=17 18=17 19=17, gen(presvote)
label define presvotetemp 1 Ciro 2 Lula 3 Roseana 4 Serra 5 Garotinho  6 Itamar 7 "Jose Maria" 8 "Rui Costa Pimenta"  9 "Fernando Henrique Cardoso" 10 "Alckmin" 11 "Buarque" 12 "Helena" 13 "Eymael" 14 "Bivar" 16 Other 17 "No One" 18 DK 19 NR
label values presvote presvotetemp

recode v22c_presvote 1=4 2=1 3=16 4=19 5=2 6=16 7=16 8=16 9=16 10=10 11=16 12=12 13=16 14=16 16=16 17=16 18=16 19=16 .=16, gen(presvote_iv)
label define presvote_iv 4 Ciro 1 Lula 19 Serra 2 Garotinho 16 "Other/No One/DK/NR"
label values presvote_iv presvote_iv
replace presvote_iv=. if wave~=1

*Variables for CV model
recode v50_ideology v61a_candsideologylula v61b_candsideologyserra v61d_candsideologyciro v61e_candsideologygaro v61f_candsideologyalckmin (6/99=.)
recode v63a_issuespriv v63b_issuesprivlula v63c_issuesprivserra v63d_issuesprivroseana v63e_issuesprivciro v63f_issuesprivgaro v63g_issuesprivalckmin  (6/99=.)
recode v69a_issuessocialspend v69b_issuessocialspendlula v69c_issuessocialspendserra v69e_issuessocialspendciro v69f_issuessocialspendgaro (6/99=.)
recode v70a_issueslandreform v70b_issueslandreformlula v70c_issueslandreformserra v70d_issueslandreformroseana v70e_issueslandreformciro v70f_issueslandreformgaro v70g_issueslandreformalckmin (6/99=.)
recode v5d_econpastbrazil v5a_econpastego  (8/9=.)
recode v41b_thermfhc (18/19=.)
recode v26a_preswin 1=1 2/19=-1 , gen(ciro_win)
recode v72a_issuesbolsaescola  v72b_issuesrendaminima  v72c_issuesop  v72d_issuesfomezero (8/9=.) (5=1) (4=2) (3=3) (2=4) (1=5)

*Remember that any vars you want to use must be put in the next two commands
keep id wave city whennew respond bairroibge presvote ///
 presvote_iv v50_ideology v61a_candsideologylula v61b_candsideologyserra v61d_candsideologyciro v61e_candsideologygaro v61f_candsideologyalckmin  ///
evangelical_attend v63a_issuespriv v63b_issuesprivlula v63c_issuesprivserra v63d_issuesprivroseana v63e_issuesprivciro v63f_issuesprivgaro ///
v69a_issuessocialspend v69b_issuessocialspendlula v69c_issuessocialspendserra v69e_issuessocialspendciro v69f_issuessocialspendgaro lnincome educ ///
woman v70a_issueslandreform v70b_issueslandreformlula v70c_issueslandreformserra v70d_issueslandreformroseana v70e_issueslandreformciro v70f_issueslandreformgaro ///
v40k_issuesthermpriv v40l_issuesthermfreetrade v40m_issuesthermdeathpenalty v40n_issuesthermlandreform v64_issuesfreetrade  ///
v65_issuesalca v22c_presvote converse v16e_conversefriends v16g_conversefamily v16b_conversenotamob  v5d_econpastbrazil v5a_econpastego  v41b_thermfhc ciro_win  ///
s14acs_know1cargo s14ajf_know1cargo s14b_know2vicepres s14c_know3fhcparty s14d_know4mercosul s14ecs_know5senator s14ejf_know5senator s14f_know6preschamber s14gcs_know7mayor s14gjf_know7mayor s14hcs_know8cargopauletti ///
s14bW4_know2vicepres s14fW5_know6preschamber s14ics_know9simonparty mediaattention s11a_raceintrvwr s11b_raceself v38a_persuadefrompartyyesorno v38b2b_persuade2frompartyname v38b3b_persuade3frompartyname v74_disc1named v75_disc2named v76_disc3named ///
lnincomebairronomr v81_lulagovpresapprove v63g_issuesprivalckmin v70g_issueslandreformalckmin v72a_issuesbolsaescola  v72b_issuesrendaminima  v72c_issuesop  v72d_issuesfomezero 

drop if wave==.

reshape wide city whennew respond bairroibge presvote ///
 presvote_iv v50_ideology v61a_candsideologylula v61b_candsideologyserra v61d_candsideologyciro v61e_candsideologygaro v61f_candsideologyalckmin  ///
evangelical_attend v63a_issuespriv v63b_issuesprivlula v63c_issuesprivserra v63d_issuesprivroseana v63e_issuesprivciro v63f_issuesprivgaro ///
v69a_issuessocialspend v69b_issuessocialspendlula v69c_issuessocialspendserra v69e_issuessocialspendciro v69f_issuessocialspendgaro lnincome educ ///
woman v70a_issueslandreform v70b_issueslandreformlula v70c_issueslandreformserra v70d_issueslandreformroseana v70e_issueslandreformciro v70f_issueslandreformgaro ///
v40k_issuesthermpriv v40l_issuesthermfreetrade v40m_issuesthermdeathpenalty v40n_issuesthermlandreform v64_issuesfreetrade ///
v65_issuesalca v22c_presvote converse v16e_conversefriends v16g_conversefamily v16b_conversenotamob v5d_econpastbrazil v5a_econpastego  v41b_thermfhc ciro_win ///
s14acs_know1cargo s14ajf_know1cargo s14b_know2vicepres s14c_know3fhcparty s14d_know4mercosul s14ecs_know5senator s14ejf_know5senator s14f_know6preschamber s14gcs_know7mayor s14gjf_know7mayor s14hcs_know8cargopauletti ///
s14bW4_know2vicepres s14fW5_know6preschamber s14ics_know9simonparty mediaattention s11a_raceintrvwr s11b_raceself v38a_persuadefrompartyyesorno v38b2b_persuade2frompartyname v38b3b_persuade3frompartyname ///
v74_disc1named v75_disc2named v76_disc3named lnincomebairronomr v81_lulagovpresapprove v63g_issuesprivalckmin v70g_issueslandreformalckmin v72a_issuesbolsaescola  v72b_issuesrendaminima  v72c_issuesop  v72d_issuesfomezero , i(id) j(wave) 

impute lnincome1 lnincome4 lnincome5 lnincomebairronomr1 if respond1==1, gen(wealth1)
impute lnincome2 lnincome4 lnincome5  lnincomebairronomr2 if whennew2==2, gen(wealth2)
impute lnincome4 lnincome1 lnincome5 lnincomebairronomr4 if respond4==1, gen(wealth4)

			*in both waves (1 and 2; 2 and 3)
gen change12=0 if respond1==1 & respond2==1 & v22c_presvote2~=3 & v22c_presvote1~=3 & v22c_presvote1~=6 & v22c_presvote1~=9 & v22c_presvote1<17 & v22c_presvote2<17
replace change12=1 if v22c_presvote1~=v22c_presvote2 & change12==0
bysort change12: tab v22c_presvote1 v22c_presvote2, m
tab change12

gen change23=0 if respond2==1 & respond3==1 & v22c_presvote2~=3 & v22c_presvote3~=3 & v22c_presvote2~=6 & v22c_presvote2~=9 & v22c_presvote2<17 & v22c_presvote3<17
replace change23=1 if v22c_presvote2~=v22c_presvote3 & change23==0
bysort change23: tab v22c_presvote2 v22c_presvote3, m
tab change23

gen change13=0 if respond1==1 & respond3==1 & v22c_presvote3~=3 & v22c_presvote1~=3 & v22c_presvote1~=6 & v22c_presvote1~=9 & v22c_presvote1<17 & v22c_presvote3<17
replace change13=1 if v22c_presvote1~=v22c_presvote3 & change13==0
bysort change13: tab v22c_presvote1 v22c_presvote3, m
tab change13

			*in both waves (5 and 6)		
gen change56=0 if respond5==1 & respond6==1 & v22c_presvote5~=9 & v22c_presvote5<17 & v22c_presvote6<17
replace change56=1 if v22c_presvote5~=v22c_presvote6 & change56==0
bysort change56: tab v22c_presvote5 v22c_presvote6, m
tab change56


*GEN THE AWARENESS VARIABLE.  Note its a little tricky because people answered a different number depending on which wave/s they were in
*Awareness 2002
gen correct1cs=0 if respond1==1 & city1==1
replace correct1cs=2 if s14acs_know1cargo1==1 & correct1cs==0 
		* Give half a point to people who said Ana Corso was a Fed. Dep. because she was a suplente for 8 months
replace correct1cs=1 if s14acs_know1cargo1==2 & correct1cs==0 
gen correct1jf=0 if respond1==1 & city1==2
replace correct1jf=1 if s14ajf_know1cargo1==2 & correct1jf==0 
gen correct2=0 if respond1==1
replace correct2=1 if s14b_know2vicepres1==2 & respond1==1
gen correct3=0 if respond1==1
replace correct3=1 if s14c_know3fhcparty1==3 & respond1==1
gen correct4=0 if respond1==1
replace correct4=1 if s14d_know4mercosul1==2 & respond1==1
gen correct5cs=0 if respond1==1 & city1==1
replace correct5cs=1 if s14ecs_know5senator1==1 & correct5cs==0
gen correct5jf=0 if respond1==1 & city1==2
replace correct5jf=1 if s14ejf_know5senator1==1 & correct5jf==0
gen correct6=0 if respond1==1
replace correct6=1 if s14f_know6preschamber1==3 & respond1==1

*this is for the fresh respondents
replace correct1cs=0 if respond2==1 & whennew1==2 & city1==1
replace correct1cs=2 if s14acs_know1cargo2==1 & respond2==1 & whennew1==2 & city1==1
replace correct1cs=1 if s14acs_know1cargo2==2 & respond2==1 & whennew1==2 & city1==1
replace correct1jf=0 if respond2==1 & whennew1==2 & city1==2
replace correct1jf=1 if s14ajf_know1cargo2==2 & respond2==1 & whennew1==2 & city1==2
replace correct2=0 if respond2==1 & whennew1==2 
replace correct2=1 if s14b_know2vicepres2==2 & respond2==1 & whennew1==2
replace correct3=0 if respond2==1 & whennew1==2 
replace correct3=1 if s14c_know3fhcparty2==3 & respond2==1 & whennew1==2
replace correct4=0 if respond2==1 & whennew1==2 
replace correct4=1 if s14d_know4mercosul2==2 & respond2==1 & whennew1==2
replace correct5cs=0 if respond2==1 & whennew1==2 & city1==1
replace correct5cs=1 if s14ecs_know5senator2==1 & respond2==1 & whennew1==2 & city1==1
replace correct5jf=0 if respond2==1 & whennew1==2 & city1==2
replace correct5jf=1 if s14ejf_know5senator2==1 & respond2==1 & whennew1==2 & city1==2
replace correct6=0 if respond2==1 & whennew1==2 
replace correct6=1 if s14f_know6preschamber2==3 & respond2==1 & whennew1==2

replace correct2=0 if respond3==1 & whennew1==3 
replace correct2=1 if s14b_know2vicepres3==2 & respond3==1 & whennew1==3
replace correct3=0 if respond3==1 & whennew1==3 
replace correct3=1 if s14c_know3fhcparty3==3 & respond3==1 & whennew1==3
replace correct4=0 if respond3==1 & whennew1==3 
replace correct4=1 if s14d_know4mercosul3==2 & respond3==1 & whennew1==3

gen correct7cs=0 if respond3==1 & city1==1
replace correct7cs=1 if s14gcs_know7mayor3==4
gen correct7jf=0 if respond3==1 & city1==2
replace correct7jf=1 if s14gjf_know7mayor3==4

irt grm correct1cs correct1jf correct2 correct3 correct4 correct5cs correct5jf correct6 correct7cs correct7jf
predict awaretemp , latent ebmeans
egen aware1z=std(awaretemp) 

*Awareness 2006
drop correct*
gen correct11cs=0 if respond5==1 & city1==1
replace correct11cs=1 if s14hcs_know8cargopauletti5==3 & correct11cs==0 
gen correct12=0 if respond5==1
replace correct12=1 if s14bW4_know2vicepres5==4 & respond5==1
gen correct14=0 if respond5==1
replace correct14=1 if s14d_know4mercosul5==2 & respond5==1
gen correct16=0 if respond5==1
replace correct16=1 if s14fW5_know6preschamber5==4 & respond5==1
gen correct15cs=0 if respond5==1 & city1==1
replace correct15cs=1 if s14ecs_know5senator5==1 & correct15cs==0
gen correct15jf=0 if respond5==1 & city1==2
replace correct15jf=1 if s14ejf_know5senator5==1 & correct15jf==0
gen correct17cs=0 if respond5==1 & city1==1
replace correct17cs=1 if s14gcs_know7mayor5==3
gen correct17jf=0 if respond5==1 & city1==2
replace correct17jf=1 if s14gjf_know7mayor5==1
gen correct13cs=0 if respond5==1 & city1==1
replace correct13cs=1 if s14ics_know9simonparty5==2

irt 2pl correct11cs correct12 correct14 correct15cs correct15jf correct16 correct17cs correct17jf correct13cs if respond5==1
predict awaretemp5 if respond5==1, latent ebmeans
egen aware2z=std(awaretemp5) if respond5==1

*race
recode s11a_raceintrvwr1 s11a_raceintrvwr2 s11a_raceintrvwr3 s11a_raceintrvwr4 s11a_raceintrvwr5 s11a_raceintrvwr6 (1=3) (2=2) (3=1) (4=2) (5=1) (8/9=.), gen(race_intrvwr1 race_intrvwr2 race_intrvwr3 race_intrvwr4 race_intrvwr5 race_intrvwr6)
recode s11b_raceself1 s11b_raceself2 s11b_raceself3 s11b_raceself4 s11b_raceself5 s11b_raceself6 (1=3) (2=2) (3=1) (4=2) (5=1) (8/9=.), gen(race_self1 race_self2 race_self3 race_self4 race_self5 race_self6)
egen racemean=rowmean(race_intrvwr1 race_self1 race_intrvwr2 race_self2 race_intrvwr3 race_self3)

*party contact
recode v38a_persuadefrompartyyesorno3 (2=0) (8/9=.)
gen partycontact3=0 if respond3==1
replace partycontact3=1 if v38a_persuadefrompartyyesorno3 ==1
replace partycontact3=2 if v38b2b_persuade2frompartyname3~=.
replace partycontact3=3 if v38b3b_persuade3frompartyname3~=.

recode v38a_persuadefrompartyyesorno6 (2=0) (8/9=.)
gen partycontact6=0 if respond6==1
replace partycontact6=1 if v38a_persuadefrompartyyesorno6==1
replace partycontact6=2 if v38b2b_persuade2frompartyname6~=.
replace partycontact6=3 if v38b3b_persuade3frompartyname6~=.

*COUNT DISCUSSANTS (IV)
egen disctotal2=rsum(v74_disc1named2 v75_disc2named2 v76_disc3named2)
replace disctotal2=. if respond2==0
replace disctotal2=. if whennew2==2

egen disctotal5=rsum(v74_disc1named5 v75_disc2named5 v76_disc3named5)
replace disctotal5=. if respond5==0

*ISSUES
mvdecode v63a_issuespriv2 v64_issuesfreetrade3 v65_issuesalca3 v70a_issueslandreform2, mv(8)
mvdecode v63a_issuespriv2 v64_issuesfreetrade3 v65_issuesalca3 v70a_issueslandreform2, mv(9)
mvdecode v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2, mv(18)
mvdecode v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2, mv(19)

egen rmean=rmean(v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2)
egen rsd=rsd(v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2)
recode rsd 0=.1
gen ftprivz= (v40k_issuesthermpriv2-rmean)/rsd
gen ftftz= (v40l_issuesthermfreetrade2-rmean)/rsd
gen ftlrz= (v40n_issuesthermlandreform2-rmean)/rsd

factor ftprivz v63a_issuespriv2  ftftz v64_issuesfreetrade3 v65_issuesalca3 ftlrz v70a_issueslandreform2 , pcf factors(1)
predict tempeconissues1
impute tempeconissues1  ftprivz v63a_issuespriv2  ftftz v64_issuesfreetrade3 v65_issuesalca3 ftlrz v70a_issueslandreform2 if respond3==1, gen(econissues)
replace econissues=-econissues

factor v72a_issuesbolsaescola4  v72b_issuesrendaminima4  v72c_issuesop4  v72d_issuesfomezero4 v69a_issuessocialspend4 , pcf factors(1)
predict tempsocialspendissues4
impute tempsocialspendissues4 v72a_issuesbolsaescola4  v72b_issuesrendaminima4  v72c_issuesop4  v72d_issuesfomezero4 v69a_issuessocialspend4 if respond4==1, gen(socialspendissues4)

*Gen correct voting Score 2002
mlogit v22c_presvote3 v63a_issuespriv1 v70a_issueslandreform1 v5d_econpastbrazil1 v41b_thermfhc1 evangelical_attend1 v50_ideology1 if v22c_presvote3<6 

	*Valence
recode v5d_econpastbrazil1 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(socio1)
summ v41b_thermfhc1 
replace v41b_thermfhc1 = ((v41b_thermfhc1 -r(min))/(r(max)-r(min)))
replace v41b_thermfhc1 =2*v41b_thermfhc1 -1

	*Positional
recode v50_ideology1 1=-1 2=-.5 3=0 4=.5 5=1, gen(ideo1) 
recode v61*_candsideology*3 (1=-1) (2=-.5) (3=0) (4=.5) (5=1)
summ v61b_candsideologyserra3 v61a_candsideologylula3 v61d_candsideologyciro3 v61e_candsideologygaro3 if aware1z>.45 & aware1z~=.

recode v63a_issuespriv1 1=1 2=.5 3=0 4=-.5 5=-1, gen(priv1)
recode v63*_issuespriv*3 (1=1) (2=.5) (3=0) (4=-.5) (5=-1) 
 summ v63c_issuesprivserra3 v63b_issuesprivlula3 v63e_issuesprivciro3 v63f_issuesprivgaro3 if aware1z>.45 & aware1z~=.

recode v70a_issueslandreform1 1=-1 2=-.5 3=0 4=.5 5=1, gen(landreform1)
recode v70*_issueslandreform*3 (1=-1) (2=-.5) (3=0) (4=.5) (5=1)
summ v70c_issueslandreformserra3 v70b_issueslandreformlula3 v70e_issueslandreformciro3 v70f_issueslandreformgaro3 if aware1z>.45 & aware1z~=.

recode evangelical_attend1 3=1 2=.33333 1=-.333333 0=-1, gen(evangelical1)

capture program drop cv
program define cv
while "`1'" ~= "" {
gen serra_`1'=-(abs(`1'-`2')-1)
gen lula_`1'=-(abs(`1'-`3')-1)
gen ciro_`1'=-(abs(`1'-`4')-1)
gen garo_`1'=-(abs(`1'-`5')-1)
mac shift
mac shift
mac shift
mac shift
mac shift
}
exit
end

cv socio1 1 -1 -1 -1 v41b_thermfhc1 1 -1 -1 -1
cv ideo1 .733 -.77 -.27 -.3975 priv1 .6848 -.708 -.1062 -.3715
cv landreform1 .12065 -.6968 -.18447 -.35699
cv evangelical1 -1 -1 -1 1

egen serra1_av=rowtotal(serra_socio1 serra_v41b_thermfhc1 serra_ideo1 serra_priv1 serra_landreform1 serra_evangelical1 )
egen lula1_av=rowtotal(lula_socio1 lula_v41b_thermfhc1 lula_ideo1 lula_priv1 lula_landreform1 lula_evangelical1 )
egen ciro1_av=rowtotal(ciro_socio1 ciro_v41b_thermfhc1 ciro_ideo1 ciro_priv1 ciro_landreform1 ciro_evangelical1 )
egen garo1_av=rowtotal(garo_socio1 garo_v41b_thermfhc1 garo_ideo1 garo_priv1 garo_landreform1 garo_evangelical1 )

gen correct_vote1=1 if ciro1_av>lula1_av & ciro1_av>serra1_av  & ciro1_av>garo1_av 
replace correct_vote1=2 if lula1_av>ciro1_av & lula1_av>serra1_av  & lula1_av>garo1_av 
replace correct_vote1=4 if serra1_av>ciro1_av & serra1_av>lula1_av  & serra1_av>garo1_av 
replace correct_vote1=5 if garo1_av>ciro1_av & garo1_av>lula1_av  & garo1_av>serra1_av 

gen voted_correctly1to2=1 if correct_vote1==v22c_presvote2 & v22c_presvote2<6
replace voted_correctly1to2=0 if correct_vote1~=v22c_presvote2 & v22c_presvote2<6
replace voted_correctly1to2=. if respond1==0 
*replace voted_correctly1to2=. if whennew2==2

gen voted_correctly1to3=1 if correct_vote1==v22c_presvote3 & v22c_presvote3<6
replace voted_correctly1to3=0 if correct_vote1~=v22c_presvote3 & v22c_presvote3<6
replace voted_correctly1to3=. if respond1==0 | respond3==0
replace voted_correctly1to3=. if whennew2==2

egen conversemean=rowmean(converse2 converse3)
egen mediaattentionmean=rowmean(mediaattention2 mediaattention3)

*Figure 8.4, Table C.1
eststo clear
eststo: logit voted_correctly1to3 aware1z i.v22c_presvote3 city1
margin, at((p5) aware1z) at((p95) aware1z) contrast(atcontrast(r)) atmeans
mat bawareBR02=r(b)
mat vawareBR02=r(V)

eststo: logit voted_correctly1to3 conversemean mediaattentionmean partycontact3 i.v22c_presvote3 city1
margin, at((p5) conversemean) at((p95) conversemean) contrast(atcontrast(r)) atmeans
mat bconverseBR02=r(b)
mat vconverseBR02=r(V)
margin, at((p5) mediaattentionmean) at((p95) mediaattentionmean) contrast(atcontrast(r)) atmeans
mat bmediaBR02=r(b)
mat vmediaBR02=r(V)
margin, at((p5) partycontact3) at((p95) partycontact3) contrast(atcontrast(r)) atmeans
mat bcontactBR02=r(b)
mat vcontactBR02=r(V)

logit voted_correctly1to3 disctotal2 mediaattentionmean partycontact3 i.v22c_presvote3 city1

eststo: logit voted_correctly1to3 wealth1 educ1 woman1 racemean i.v22c_presvote3 city1 
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) atmeans
mat bsesBR02=r(b)
mat vsesBR02=r(V)

	*Change stuff
*Table 8.1
logit voted_correctly1to3 change23 voted_correctly1to2 i.v22c_presvote3 city2

tab voted_correctly1to2 
tab voted_correctly1to3 

tab voted_correctly1to2 if voted_correctly1to3~=. 
tab voted_correctly1to3 if voted_correctly1to2~=. 

prop voted_correctly1to2 if voted_correctly1to3~=. 
prop voted_correctly1to3 if voted_correctly1to2~=. 

*Gen correct voting Score 2006
mlogit v22c_presvote6 v63a_issuespriv4 v81_lulagovpresapprove4 v70a_issueslandreform4 v5d_econpastbrazil4 v50_ideology4 if v22c_presvote6<18 

	*Valence
recode v5d_econpastbrazil4 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(socio4)
recode v81_lulagovpresapprove4 5=-1 4=-.5 3=0 2=.5 1=1 8/9=., gen(lula_approve4)  

	*Positional
recode v50_ideology4 1=-1 2=-.5 3=0 4=.5 5=1, gen(ideo4) 
recode v61*_candsideology*6 (1=-1) (2=-.5) (3=0) (4=.5) (5=1) (else=.)
summ v61a_candsideologylula6 v61f_candsideologyalckmin6 if aware2z>.5 & aware2z~=.

recode v63a_issuespriv4 1=1 2=.5 3=0 4=-.5 5=-1, gen(priv4)
recode v63*_issuespriv*6 (1=1) (2=.5) (3=0) (4=-.5) (5=-1) (else=.)
summ v63b_issuesprivlula6 v63g_issuesprivalckmin6 if aware2z>.5 & aware2z~=.

recode v70a_issueslandreform4 1=-1 2=-.5 3=0 4=.5 5=1, gen(landreform4)
recode v70*_issueslandreform*6 (1=-1) (2=-.5) (3=0) (4=.5) (5=1) (else=.)
summ v70b_issueslandreformlula6 v70g_issueslandreformalckmin6  if aware2z>.5 & aware2z~=.

*recode evangelical_attend1 3=1 2=.33333 1=-.333333 0=-1, gen(evangelical1)

*from W-B, tax vs spend
display -1.1052+.1052*7.2 /*Lula*/
display -1.1052+.1052*13.027  /*PSDB*/
display -1.1052+.1052*3.19 /*PSOL*/

*from W-B, privatization
display -1.1052+.1052*7.5 /*Lula*/
display -1.1052+.1052*16.86  /*PSDB*/
display -1.1052+.1052*4.76 /*PSOL*/

*from W-B, ideology
display -1.1052+.1052*8.15 /*Lula*/
display -1.1052+.1052*13.46  /*PSDB*/
display -1.1052+.1052*2.954 /*PSOL*/

capture program drop cv
program define cv
while "`1'" ~= "" {
gen lula_`1'=-(abs(`1'-`2')-1)
gen alckmin_`1'=-(abs(`1'-`3')-1)
gen helena_`1'=-(abs(`1'-`4')-1)
mac shift
mac shift
mac shift
mac shift
}
exit
end

*cv socio4 1 -1 lula_approve4 1 -1
*cv ideo4 -.4713 .537 priv4 -.578 .4376
*cv landreform4 -.433 .242 

cv socio4 1 -1 -1 lula_approve4 1 -1 -1
cv ideo4 -.24782 .310792 -.79443 priv4 -.3162 .66947 -.604448
cv landreform4 -.3477 .26524 -.76912

egen lula4_av=rowtotal(lula_socio4 lula_lula_approve4 lula_ideo4 lula_priv4 lula_landreform4 )
egen alckmin4_av=rowtotal(alckmin_socio4 alckmin_lula_approve4 alckmin_ideo4 alckmin_priv4 alckmin_landreform4 )
egen helena4_av=rowtotal(helena_socio4 helena_lula_approve4 helena_ideo4 helena_priv4 helena_landreform4 )

*gen correct_vote4=2 if lula4_av>alckmin4_av 
*replace correct_vote4=10 if alckmin4_av>lula4_av 

gen correct_vote4=2 if lula4_av>alckmin4_av & lula4_av>helena4_av
replace correct_vote4=10 if alckmin4_av>lula4_av & alckmin4_av>helena4_av
replace correct_vote4=12 if helena4_av>lula4_av & helena4_av>alckmin4_av

recode v22c_presvote6 11=.
gen voted_correctly4to6=1 if correct_vote4==v22c_presvote6 & v22c_presvote6<13
replace voted_correctly4to6=0 if correct_vote4~=v22c_presvote6 & v22c_presvote6<13
replace voted_correctly4to6=. if respond4==0 | respond6==0

gen voted_correctly4to5=1 if correct_vote4==v22c_presvote5 & v22c_presvote5<13
replace voted_correctly4to5=0 if correct_vote4~=v22c_presvote5 & v22c_presvote5<13
replace voted_correctly4to5=. if respond4==0 | respond5==0

drop conversemean mediaattentionmean
egen conversemean=rowmean(converse5 converse6)
egen mediaattentionmean=rowmean(mediaattention5 mediaattention6)

*Figure 8.4, Table C.1
eststo: logit voted_correctly4to6 aware2z i.v22c_presvote6 city1
margin, at((p5) aware2z) at((p95) aware2z) contrast(atcontrast(r)) atmeans
mat bawareBR06=r(b)
mat vawareBR06=r(V)

eststo: logit voted_correctly4to6 conversemean mediaattentionmean partycontact6 i.v22c_presvote6 city1
margin, at((p5) conversemean) at((p95) conversemean) contrast(atcontrast(r)) atmeans
mat bconverseBR06=r(b)
mat vconverseBR06=r(V)
margin, at((p5) mediaattentionmean) at((p95) mediaattentionmean) contrast(atcontrast(r)) atmeans
mat bmediaBR06=r(b)
mat vmediaBR06=r(V)
margin, at((p5) partycontact6) at((p95) partycontact6) contrast(atcontrast(r)) atmeans
mat bcontactBR06=r(b)
mat vcontactBR06=r(V)

logit voted_correctly4to6 disctotal5 mediaattentionmean partycontact6 i.v22c_presvote6 city1

replace wealth1=wealth2 if wealth1==.
replace educ1=educ2 if educ1==.
replace educ1=educ3 if educ1==.
replace woman1=woman2 if woman1==.
replace woman1=woman3 if woman1==.

eststo: logit voted_correctly4to6 wealth1 educ1 woman1 racemean i.v22c_presvote6 city1 
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) atmeans
mat bsesBR06=r(b)
mat vsesBR06=r(V)

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Output\"
esttab est1 est2 est3 est4 est5 est6 using Brazil200206CV.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(CV Brazil 02)
xyz
	*Change stuff
*Table 8.1
logit voted_correctly4to6 change56 voted_correctly4to5 i.v22c_presvote6 city2


tab voted_correctly4to5 
tab voted_correctly4to6 

tab voted_correctly4to5 if voted_correctly4to6~=. 
tab voted_correctly4to6 if voted_correctly4to5~=. 

prop voted_correctly4to5 if voted_correctly4to6~=. 
prop voted_correctly4to6 if voted_correctly4to5~=. 

	*MAKE FIGURE, ENTER RESULTS FROM MEXICO 2006 AND BRAZIL 2014 BELOW-----------------------------
eststo clear
*MEX06 panel: drawn from Chapter 8 CV Mexico
mat bawareMX06= .1350354
mat vawareMX06=.0519713^2
mat bconverseMX06= .2222174
mat vconverseMX06=.0666308^2
mat bmediaMX06= .0103035
mat vmediaMX06=.0649409^2 
mat bcontactMX06=.00915811
mat vcontactMX06= .0626113^2
mat bsesMX06=.1762815 
mat vsesMX06=.0663813^2

*BRA14: drawn from Chapter 8 CV Mexico
mat bawareBR14= .166741 
mat vawareBR14=.0618997^2
mat bconverseBR14=.1737197
mat vconverseBR14=.0611451^2 
mat bmediaBR14= .0428109 
mat vmediaBR14=.0698494^2 
mat bsesBR14= .0294479
mat vsesBR14= .0703903^2 

mat Baware=[bawareMX06\bawareBR14\bawareBR06\bawareBR02]
mat Vaware=[vawareMX06\vawareBR14\vawareBR06\vawareBR02]

mat Bconverse=[bconverseMX06\bconverseBR14\bconverseBR06\bconverseBR02]
mat Vconverse=[vconverseMX06\vconverseBR14\vconverseBR06\vconverseBR02]

mat Bmedia=[bmediaMX06\bmediaBR14\bmediaBR06\bmediaBR02]
mat Vmedia=[vmediaMX06\vmediaBR14\vmediaBR06\vmediaBR02]

mat Bcontact=[bcontactMX06\.\bcontactBR06\bcontactBR02]
mat Vcontact=[vcontactMX06\.\vcontactBR06\vcontactBR02]

mat Bses=[bsesMX06\bsesBR14\bsesBR06\bsesBR02]
mat Vses=[vsesMX06\vsesBR14\vsesBR06\vsesBR02]

svmat Baware 
svmat Bconverse 
svmat Bmedia 
svmat Bcontact
svmat Bses 

svmat Vaware 
svmat Vconverse 
svmat Vmedia 
svmat Vcontact
svmat Vses 

gen n=_n

replace n=. if n>5

gen nconverse=n-.225
gen nmedia=n+-.375
gen ncontact=n-.525
gen nses=n-.75

gen awareupper=Baware+1.9645*(Vaware^.5)
gen awarelower=Baware-1.9645*(Vaware^.5)

gen converseupper=Bconverse+1.9645*(Vconverse^.5)
gen converselower=Bconverse-1.9645*(Vconverse^.5)

gen mediaupper=Bmedia+1.9645*(Vmedia^.5)
gen medialower=Bmedia-1.9645*(Vmedia^.5)

gen contactupper=Bcontact+1.9645*(Vcontact^.5)
gen contactlower=Bcontact-1.9645*(Vcontact^.5)

gen sesupper=Bses+1.9645*(Vses^.5)
gen seslower=Bses-1.9645*(Vses^.5)

*Figure 8.4
*no legend
twoway (rcap awareupper awarelower n if n<5, color(black) horizontal ) (scatter n Baware if n<5, mcolor(black)) ///
(rcap converseupper converselower nconverse if n<5, color(black) horizontal ) (scatter nconverse Bconverse if n<5, mcolor(black) msymbol(S)) ///
(rcap mediaupper medialower nmedia if n<5, color(black) horizontal ) (scatter nmedia Bmedia if n<5, mcolor(black) msymbol(S)) ///
(rcap contactupper contactlower ncontact if n<5, color(black) horizontal ) (scatter ncontact Bcontact if n<5, mcolor(black) msymbol(S)) ///
(rcap sesupper seslower nses if n<5, color(black) horizontal ) (scatter nses Bses if n<5, mcolor(black) msymbol(t) msize(medlarge)), ///
xtitle("{bf:Model-predicted Change in Probability of Voting Correctly}" "(upon changing IV from 5th to 95th percentile)", color(black)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Country, Year, and Independent Variable (IV)}", size(medium)) ///
legend(order(2 "Political Knowledge Models" 4 "Political Intermediation Models" 10 "Societal Exclusion Models") rows(1) size(small) span) title("{bf:Dependent Variable: Voted Correctly}", size(medsmall) color(black)) ///
ytick(1.125 2.125 3.125, tlength(28) tlwidth(medium)) ///
xline(0, lcolor(black)) ymlab(1 "Pol. knowledge" 2 "Pol. knowledge" 3 "Pol. knowledge" 4 "Pol. knowledge" ///
.25 "SES" 1.25 "SES" 2.25 "SES" 3.25 "SES" ///
.475 "Contact by party" 2.475 "Contact by party" 3.475 "Contact by party" ///
3.625 "Media exposure" 1.625 "Media exposure" 2.625 "Media exposure" .625 "Freq. of TV news" ///
.775 "Freq. of pol. disc." 1.775 "Freq. of pol. disc." 2.775 "Freq. of pol. disc." 3.775 "Freq. of pol. disc." ///
, angle(horizontal) labsize(small) tlcolor(black)) ///
ylab(.625 "MEX06" 1.625 "BRA14" 2.625 "BRA06" 3.625 "BRA02", nogrid angle(vertical) notick labgap(24) labsize(medium)) xscale(range(-.1 .4)) xlab(-.1 0 .1 .2 .3 .4, grid) ///
yline(1.125 2.125 3.125, lcolor(black)) ///
text(4 .37 "{it:N}=2,934", size(small)) text(3.625 .37 "{it:N}=2,934", size(small)) text(3.25 .37 "{it:N}=2,869", size(small)) ///
text(3 .37 "{it:N}=1,343", size(small)) text(2.625 .37 "{it:N}=1,344", size(small)) text(2.25 .37 "{it:N}=1,316", size(small)) ///
text(2 .37 "{it:N}=996", size(small)) text(1.625 .37 "{it:N}=992", size(small)) text(1.25 .37 "{it:N}=984", size(small)) ///
text(1 .37 "{it:N}=1,217", size(small)) text(.625 .37 "{it:N}=1,212", size(small)) text(.25 .37 "{it:N}=1,211", size(small))


graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Chapter 8 CV 1.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_4.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_4.pdf", as(pdf) replace 


mat bra02=[46.66\50.98]
mat bra06=[46.25\48.88]
mat bra14=[50.88\53.53]
mat mex06=[52.51\56.52]

svmat bra02
svmat bra06
svmat bra14
svmat mex06

*Figure 8.5
twoway (line bra02 bra06 bra14 mex06 n, lcolor(black black black black)) (scatter bra02 bra06 bra14 mex06 n, color(black black black black) symbol(circle square triangle diamond)) if n<2.5, ///
xlab(1 "Campaign-wave vote intention" 2 "Election-day vote choice") ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Percent Voting Correctly}", size(medium)) ///
legend(off) xscale(range(.7 2.3)) ylab(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%" 60 "60%") xtitle("{bf:Timing}", size(medium)) ///
text(53.7 2.02 "Brazil 2014", place(east)) text(56.52 2.02 "Mexico 2006", place(east) color(black)) text(49.05 2.02 "Brazil 2006", place(east)) text(51.1 2.02 "Brazil 2002", place(east))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Chapter 8 CV rates.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_5.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_5.pdf", as(pdf) replace 


xyz


